<template>
  <!-- 预览费用(就诊) -->
  <el-dialog title="预览费用" :visible="value" width="800px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <div v-loading="loading">
      <h4 style="margin:0">收费项目</h4>
      <br />
      <table class="grid-table">
        <thead>
          <tr>
            <th>名称</th>
            <th>单位</th>
            <th>单价</th>
            <th>数量</th>
            <th>金额</th>
            <th>折扣</th>
            <th>折后金额</th>
            <th>医生</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item,index) in tableList" :key="index">
            <td>{{item.sourceName}}</td>
            <td style="white-space: nowrap;">{{item.unitName}}</td>
            <td style="white-space: nowrap;">{{item.unitPrice}}</td>
            <td style="white-space: nowrap;">{{item.num}}</td>
            <td style="white-space: nowrap;">{{item.totalPrice}}</td>
            <td style="white-space: nowrap;">{{item.discounts*100}}%</td>
            <td style="white-space: nowrap;">{{item.discountsMoney|toFixed}}</td>
            <td style="white-space: nowrap;">{{item.doctorName}}</td>
          </tr>
        </tbody>
      </table>
      <br />
      <div class="form-tool">
        <div></div>
        <div style="font-size:18px;">
          <div class="form-tool" style="margin-bottom:5px;">
            <div>费用合计：</div>
            <div style="width:150px;">
              <span class="error-color">￥{{fee.total|toFixed}}</span>
            </div>
          </div>
          <div class="form-tool" style="margin-bottom:5px;">
            <div>折扣金额：</div>
            <div style="width:150px;">
              <span class="error-color">￥{{fee.discount|toFixed}}</span>
            </div>
          </div>
          <div class="form-tool" style="margin-bottom:5px;">
            <div>应收费用：</div>
            <div style="width:150px;">
              <span class="error-color">￥{{fee.should|toFixed}}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "cureId"],
  computed: {
    patientInfo() {
      return this.$store.state.doctorMZ.patientInfo;
    }
  },
  filters: {
    toFixed(value) {
      if (value) return value.toFixed(2);
      else return "0.00";
    }
  },
  data() {
    return {
      loading: false,
      tableList: [],
      fee: {
        total: 0,
        discount: 0,
        should: 0
      },
      levelData: null,
      discountEnum: []
    };
  },
  created() {
    let th = this;
    th.getCommonConfigByKey("会员折扣关联").then(res => {
      th.discountEnum = res;
    });
  },
  methods: {
    getData() {
      let th = this;
      let j = {
        cureId: th.cureId,
        time: new Date().getTime()
      };
      th.$api.findNoPayDetailList(j).then(res => {
        if (res.executed) {
          //   th.tableList = res.data;
          th.disposeData(res.data);
        }
        th.loading = false;
      });
    },
    //处理数据
    disposeData(arr) {
      let th = this;
      arr.forEach(d => {
        d.discounts = 1; //折扣率
        d.discountsMoney = d.totalPrice;
        if (d.isDiscount == 1 && th.levelData) {
          //获取折扣
          d.discounts = th.getDiscounts(d.source);
          d.discountsMoney = d.totalPrice * d.discounts;
        }
        th.fee.total += d.totalPrice;
        th.fee.should += d.discountsMoney;
      });
      //折扣金额
      th.fee.discount = th.fee.total - th.fee.should;
      th.tableList = arr;
    },
    //获取折扣
    getDiscounts(source) {
      let th = this;
      let value = 1;
      let typeId;
      //获取source属于什么分类
      for (let i = 0; i < th.discountEnum.length; i++) {
        let data = th.discountEnum[i];
        let index = data.desc.indexOf(source + "");
        if (index != -1) {
          typeId = data.id;
          break;
        }
      }
      if (typeof typeId == "number" && th.levelData.levelProjectRelation) {
        //获取折扣率
        let data = th.levelData.levelProjectRelation.filter(d => {
          return d.projectId == typeId;
        });
        // console.log(data);
        if (data.length > 0) {
          value = (data[0].discount / 100).toFixed(1);
        }
      }
      return value;
    },
    async getDataInfo() {
      let th = this;
      let j = { id: th.patientInfo.levelId, hpId: th.adminUser.hpId };
      th.loading = true;
      if (th.patientInfo.levelId) {
        th.levelData = await th.$api.getLevelEnumById(j);
      }
      th.getData();
    },
    onOpen() {
      let th = this;
      th.getDataInfo();
    },
    onClose() {
      let th = this;
      th.tableList = [];
      th.fee = {
        total: 0,
        discount: 0,
        should: 0,
        true: 0
      };
      th.$emit("input", false);
    }
  }
};
</script>